package servlet.dept;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import dao.DepartmentDao;
import dbutil.DBHelper;
import entity.Department;

public class DelDeptServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public DelDeptServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		JSONObject jsonObject = new JSONObject();
		DepartmentDao departmentDao = null;
		Connection conn = null;
		
		try {
			conn = DBHelper.getConnection();
			conn.setAutoCommit(false);
			departmentDao = new DepartmentDao();
			
			if(departmentDao.delOneDepartment(request.getParameter("deptName"))) {// 删除成功
				jsonObject.put("retCode", "1");
				conn.commit();
			} else {// 删除失败
				jsonObject.put("retCode", "0");
				jsonObject.put("desc", "删除失败，检查该部门是否存在人员");
				conn.rollback();
			}
			
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put("retCode", "0");
			jsonObject.put("desc", "DelDeptServlet Exception");
		} finally {
			out.print(jsonObject.toString());
			out.close();
		}
	}

	public void init() throws ServletException {
	}

}